package com.ndood.core.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.lang3.StringUtils;

public class ExceptionUtil {

	/**
	 * 获取异常的堆栈信息
	 * 
	 * @param t
	 * @return
	 */
	public static String getStackTrace(Throwable t) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		try {
			t.printStackTrace(pw);
			return sw.toString();
		} finally {
			pw.close();
		}
	}
	
	/**
	 * 打印异常信息
	 * @param e
	 * @param desc
	 * @return
	 */
	public static String printStackTrace(Throwable e, String desc){
		return desc + printStackTrace(e);
	}
	
	/**
	 * 打印异常信息
	 * @param e
	 * @return
	 */
	public static String printStackTrace(Throwable e){
		// StackTraceElement[] elements = e.getStackTrace();
		int temp = 0;
		String msg = e.getMessage()==null?e.getClass().getName().substring(e.getClass().getName().lastIndexOf(".")+1):e.getMessage();
		return msg+","+e.getStackTrace()[temp].getFileName().replace(".java", "")+"-"+e.getStackTrace()[temp].getLineNumber();
	}
	
	/**
	 * Java异常信息打印
	 * @param method
	 * @param e
	 * @param strings
	 * @return
	 */
	public static String printErrorCall(String method, Exception e, Object ... strings){
		StringBuffer sb = new StringBuffer(method).append("(");
		for (Object string : strings) {
			if(string==null||StringUtils.isBlank(string.toString()))
				continue;
			sb.append(",").append(string);
		}
		sb.append(")");
		sb.append(" throws ");
		StackTraceElement[] elements = e.getStackTrace();
		for (StackTraceElement element : elements) {
			sb.append(e.getClass().getName().substring(e.getClass().getName().lastIndexOf(".")+1))
			.append(" at ")
			.append((element.getFileName()+"").replace(".java", ""))
			.append(" line ")
			.append(element.getLineNumber())
			.append(" : ");
		}
		if(!StringUtils.isEmpty(e.getMessage())){
			sb.append(" width msg ").append(e.getMessage());
		}
		System.out.println(sb.toString());
		return sb.toString().replaceFirst(",","");
	}
	
	/**
	 * 打印错误信息
	 * @param string
	 * @return
	 */
	public static String printErrorResult(String prefix, Object ... strings) {
		StringBuffer sb = new StringBuffer(prefix);
		
		int isSet = 0;
		if(strings.length>0){
			for(int i=strings.length-1;i>=0;i--){
				if(strings[i]!=null){
					sb.append(strings[i]);
					isSet=1;
					break;
				}
			}
		}
		if(isSet==0){
			sb.append("系统错误");
		}
		return sb.toString();
	}
	
	public static void main(String[] args) throws Exception {
		try {
			test1();
		} catch (Exception e) {
			System.out.println(ExceptionUtil.printStackTrace(e));
		}
		
	}

	private static void test1() throws Exception {
		test2();
	}

	private static void test2() throws Exception {
		try {
			test3();
		} catch (Exception e) {
			throw new Exception("111111");
		}
	}

	private static void test3() throws Exception {
		throw new Exception("asdfasdfasdfs");
	}
	
}
